package com.base.search.impl.binarysearch;

/**
 * @ClassName: MySqrt
 * @Description: 69. x 的平方根
 * @author: li
 * @Date: 2021/8/24 8:21 下午
 */
public class MySqrt {
    public int mySqrt(int x) {
        int left = 0,right = x;
        while (left<=right){
            int mid = left + ((right - left)>>1);
            //乘可能会导致超出范围
            long mul = (long) mid*mid;
            if (mul == x){
                return mid;
            }else if (mul>x){
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return right;

    }
}
